package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.ClubInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/28.
 */
public class SheTuanAdapter extends BaseAdapter {
    private Context mContext;
    private List<ClubInfo> mList = new ArrayList<ClubInfo>();

    public SheTuanAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<ClubInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shetuan_item, null);
            childHolder.mSheTuanLogo = (ImageView) convertView.findViewById(R.id.shetuan_logo);
            childHolder.mSheTuanName = (TextView) convertView.findViewById(R.id.shetuan_name);
            childHolder.mSheTuanFans = (TextView) convertView.findViewById(R.id.shetuan_renshu);
            childHolder.mSheTuanTieZi = (TextView) convertView.findViewById(R.id.shetuan_talknum);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        ClubInfo cinfo = mList.get(position);
        if (cinfo != null) {
            String headImgUrl = cinfo.getClubLogo();
            if(headImgUrl != null && headImgUrl.length()>0){
                ViewUtil.Size size =  ViewUtil.getHeadImageSize(mContext,50);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headImgUrl,size));
                if (cacheBitmap != null){
                    childHolder.mSheTuanLogo.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.mSheTuanLogo.setImageResource(R.drawable.default_head_image);
                    childHolder.mSheTuanLogo.setTag(headImgUrl);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.mSheTuanLogo, headImgUrl, size);
                }
            }else{
                childHolder.mSheTuanLogo.setImageResource(R.drawable.default_head_image);
            }

            childHolder.mSheTuanName.setText(cinfo.getClubName());
            childHolder.mSheTuanFans.setText(cinfo.getMemberCount());
            childHolder.mSheTuanTieZi.setText(cinfo.getContentCount());

        }

        return convertView;
    }

    class ChildHolder {
        ImageView mSheTuanLogo;
        TextView mSheTuanName;
        TextView mSheTuanFans;
        TextView mSheTuanTieZi;
    }
}